package com.chinanet.utils.telnet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ManagerString {
	
	public Map<String,Object> getname(String text){
		Map<String,Object> out=new HashMap<String, Object>();
		text=text.replaceAll("---- More ----", "");
		String righttext=getPatternTxPower(text).trim();	
		String[] Pattentext=righttext.split("\r\n");
		System.out.println(Pattentext.length);
		List<String> middle=new ArrayList<>();
		for(String x:Pattentext){
			middle.add(x.trim());
		}
		List<List<String>> data=new ArrayList<>();
		//unselectedPortsName
		List<String> portname=new ArrayList<>();
		//selectedports
		int i=0;
		for(String one:middle){
			String[] singal=one.split("\\s+ ");
			List<String> orgindata=new ArrayList<String>();
			for(String a:singal){
			    orgindata.add(a.trim());	    
			}
			if(!orgindata.get(3).equals("0")){
				i++;
			}
			if(!orgindata.get(4).equals("0")){
				String a="";
				for(String sin:orgindata){
					a+=sin+"   ";
				}
				portname.add(a);
			}
			data.add(orgindata);
		}
		double rate=i*1.0/(i+portname.size());
		String Portsname="";
		if(portname!=null){
		for(String b:portname){
			Portsname+=b;
		}
		}
		
		out.put("portname", Portsname);
		out.put("rate", rate);
		return out;
	}
	
	private  String getPatternTxPower(String txt) {
		String returnBack = "";
		String line = "\\--------------------------------------------------------------------------------";
//		String last="<TZ-XJ-DSW-1.MAN.H3C12510F>";
		String last="<";
		returnBack = returnMatcher(line, last, txt);
		return returnBack;
	}
	
	
	private String returnMatcher(String leftPart, String rightPart, String txt) {
		String returnBack = "";
		String regex = leftPart + "([\\s\\S].*)" + rightPart;
		Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE
				| Pattern.DOTALL);
		Matcher matcher = pattern.matcher(txt);

		while (matcher.find()) {
			returnBack = matcher.group(1);
		}

		return returnBack;
	}

}
